package day9.count_down_latch_demo;

import java.util.concurrent.CountDownLatch;
/**
 * 计时器
 */
public class Main {
    // count: 计数器为 3 个，只有 3 个全部报到了，门闩才会打开
    static CountDownLatch countDownLatch = new CountDownLatch(3);
    /**
     * 多线程下载器:
     * 线程 1: 下载0-100
     * countDownLatch.countDown();
     * 线程 2: 下载 101-200
     * countDownLatch.countDown();
     * .......
     * 线程 n: 下载..
     * countDownLatch.countDown();
     * 主线程，等所有线程全部下载完成,代表整个文件下载完成了
     * 等在countDownLatch.await()-共 n个
     */

    static class MyThread extends Thread {
        @Override
        public void run() {
            countDownLatch.countDown();
            countDownLatch.countDown();
            countDownLatch.countDown();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        MyThread t = new MyThread();
        t.start();

        countDownLatch.await(); //只有计时结束，才能执行下面的代码

        System.out.println("门闩被打开了");
    }
}
